package com.hinnotek.warehouse.ui

import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.hinnotek.warehouse.BR
import com.hinnotek.warehouse.R
import com.hinnotek.warehouse.bean.MachineDetailBean
import com.hinnotek.warehouse.databinding.ActivityWarehouseStockBinding
import com.hinnotek.warehouse.databinding.ItemMachineListLayoutBinding
import com.hinnotek.warehouse.viewmodel.WarehouseViewModel
import com.lee.common.adapter.binding.SimpleBindingAdapter
import com.lee.common.base.BaseActivity
import com.lee.common.ext.init
import com.lee.common.observer.DefaultObserver
import com.lee.common.utils.ACTIVITY_WAREHOUSE_STOCK_NUMBER_DETAIL
import dagger.hilt.android.AndroidEntryPoint


/**
 * 库存详情
 */
@Route(path = ACTIVITY_WAREHOUSE_STOCK_NUMBER_DETAIL)
@AndroidEntryPoint
class WarehouseStockActivity : BaseActivity<ActivityWarehouseStockBinding>() {

    private lateinit var adapter: SimpleBindingAdapter<MachineDetailBean, ItemMachineListLayoutBinding>
    private val viewModel by viewModels<WarehouseViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_warehouse_stock)
        setTitle("库存详情")
        bindingView.vm = viewModel
        bindingView.executePendingBindings()
        viewModel.attachLoading(loadingState)
        viewModel.dialogMsg.observe(this, DefaultObserver(this))
        viewModel.wareId = intent.getIntExtra("wareId",0)
        viewModel.autoRefresh()
    }

    override fun initVew() {
        adapter = SimpleBindingAdapter(
            R.layout.item_warehouse_stock_layout,
            ArrayList(),
            BR.machineList
        )
        bindingView.recycler.init()
        bindingView.recycler.adapter = adapter

        emptyView.adapter = adapter
        viewModel.isEmpty.observe(this, Observer {
            emptyView.values(it)
        })
        emptyView.getErrView().setOnClickListener {
            viewModel.refresh()
        }
    }

    override fun setData() {
        //列表数据
        viewModel.warehouseNumPage.observe(this, {
            adapter.addAll(it, viewModel.isFirst)
        })
    }

}